package logic

import (
	"context"
	"order/api/internal/util"
	"order/orderrpc/orderrpcclient"

	"order/api/internal/svc"
	"order/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type PayLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewPayLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PayLogic {
	return &PayLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *PayLogic) Pay(req *types.PayRequest) (resp *types.PayResponse, err error) {
	// todo: add your logic here and delete this line
	res, _ := l.svcCtx.Order.Pay(context.Background(), &orderrpcclient.PayRequest{
		Cn: req.Cn,
	})
	url := util.Pay(req.Cn, int(res.Count))
	re, _ := l.svcCtx.Order.Status(context.Background(), &orderrpcclient.StatusRequest{
		Name: res.Name,
	})
	if re.Code != 200 {
		return &types.PayResponse{
			Code: int(re.Code),
			Msg:  re.Msg,
			Data: "",
		}, nil
	}
	return &types.PayResponse{
		Code: int(re.Code),
		Msg:  re.Msg,
		Data: url,
	}, nil
}
